﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WindowsService.Common
{
    public class LogHelper
    {
        /// <summary>
        /// 获取程序异常信息
        /// </summary>
        /// <param name="methodBase"></param>
        /// <param name="exception"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string GetExceptionMessage(MethodBase methodBase, Exception exception, string message)
        {
            string fullName = methodBase.ReflectedType.FullName;
            string name = methodBase.Name;
            string str = $"\r\n异常综合信息（类：{fullName}；函数名称：{name}；）：\r\n";
            str += "-----------\r\n";
            str += ".Net异常信息：\r\n";
            if (exception == null)
            {
                str += "   无异常对象，也无堆栈信息(exception == null)\r\n";
            }
            else
            {
                str += $"   {exception.Message}\r\n";
                str += $".Net堆栈信息：\r\n{exception.StackTrace}\r\n";
            }
            str += "-----------\r\n";
            if (message != null && message.Trim().Length > 0)
            {
                str += "===========\r\n";
                str += $"自定义信息：\r\n   {message}\r\n";
                str += "===========\r\n";
            }
            return str;
        }

        /// <summary>
        /// 写出日志信息 目录地址：string logPath = AppDomain.CurrentDomain.BaseDirectory + "00_Log\\";
        /// </summary>
        /// <param name="folderName"></param>
        /// <param name="message"></param>
        public static void Write(string folderName, string message)
        {
            string text = AppDomain.CurrentDomain.BaseDirectory + "00_Log\\";
            if (folderName != null && folderName.Trim().Length > 0)
            {
                text += folderName;
            }
            WritingLogs(text, message);
        }

        /// <summary>
        ///  写出异常日志(.txt)
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="strContent"></param>
        public static void WritingLogs(string strPath, string strContent)
        {
            FileStream fileStream = null;
            StreamWriter streamWriter = null;
            try
            {
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                strPath = string.Format("{0}\\{1}{2}", strPath, DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
                fileStream = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);
                streamWriter = new StreamWriter(fileStream);
                streamWriter.BaseStream.Seek(0L, SeekOrigin.End);
                streamWriter.WriteLine(strContent + "\r\n\r\n--------------------------------------------------------" + DateTime.Now.ToString() + "--------------------------------------------------------\r\n");
                streamWriter.Flush();
                streamWriter.Close();
                fileStream.Close();
            }
            finally
            {
                streamWriter.Close();
                fileStream.Close();
            }
        }


    }
}
